﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class UserDal
    {
        DBHepler helper = new DBHepler();
        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool AddUser(Model.User user)
        {
            SqlParameter[] parameters ={
                                         new SqlParameter("@usrId",user.usrId),
                                         new SqlParameter("@roleId",user.roleId),
                                         new SqlParameter("@usrName",user.usrName),
                                         new SqlParameter("@usrPwd",user.usrPwd),
                                         new SqlParameter("@usrState",user.usrState)
                                      };
            if (helper.InsertByProc("sp_User_Add", parameters) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 查询所有的用户信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllUser()
        {
            return helper.GetDataTableByProc("sp_User_SelectAll");
        }

        /// <summary>
        /// 根据用户的角色来查询用户信息
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public DataTable GetUserByRole(int roleId)
        {
            SqlParameter[] parameters = {
                                            new SqlParameter("@roleId",roleId)
                                        };
            return helper.GetDataTableByProc("sp_User_SelectByRole", parameters);
        }
        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool UpdateUser(Model.User user)
        {
            SqlParameter[] parameters = {
                                            new SqlParameter("@roleId",user.roleId),
                                            new SqlParameter("@usrId",user.usrId),
                                            new SqlParameter("@usrName",user.usrName),
                                            new SqlParameter("@usrState",user.usrState)
                                        };
            if (helper.UpdateByProc("sp_User_UpdateUser", parameters) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="usrId"></param>
        /// <returns></returns>
        public bool DeleteUser(string usrId)
        {
            SqlParameter[] parameters = {
                                            new SqlParameter("@usrId",usrId)
                                        };
            if (helper.UpdateByProc("sp_User_DeleteUser", parameters) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 此用户是否存在
        /// </summary>
        /// <param name="usrId"></param>
        /// <returns></returns>
        public bool Exists(string usrId)
        {
            SqlParameter[] parameters ={
                                            new SqlParameter("@usrId",usrId)
                                       };
            int result = int.Parse(helper.GetExecuteScalar("sp_User_Exists",parameters));

            if (result != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到 用户的密码
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public string GetPassword(string userId)
        {
            SqlParameter[] parameteres = {
                                            new SqlParameter("@userId",userId)
                                         };
            string result = helper.GetExecuteScalar("sp_GetPasswordByUserId", parameteres).ToString();
            return result;
        }

        /// <summary>
        /// 获取指定用户的信息
        /// </summary>
        /// <param name="usrId"></param>
        /// <returns></returns>
        public DataTable GetUser(string usrId)
        {
            SqlParameter[] parameters = {
                                            new SqlParameter("@usrId",usrId)
                                        };
            return helper.GetDataTableByProc("sp_GetUserById",parameters);
        }

        public bool UpdateUserState(string userId)
        {
            SqlParameter[] parameters ={
                                        new SqlParameter("@userId",userId)
                                      };
            if (helper.UpdateByProc("sp_UpdateUserState", parameters) == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int GetStateByUser(string userId)
        {
            SqlParameter[] parameters = {
                                            new SqlParameter("@userId",userId)
                                        };
            string result = helper.GetExecuteScalar("sp_GetStateByUser", parameters).ToString();
            return int.Parse(result);
        }

        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public bool UpateUserPassword(string userId, string p)
        {
            SqlParameter[] parameters = {
                                            new SqlParameter("@userId",userId),
                                            new SqlParameter("@password",p)
                                        };
            if (helper.UpdateByProc("sp_UpdatePassword", parameters) == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
